/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.hanfu.domain.HanfuType;
import top.continew.admin.hanfu.domain.dto.HanfuTypeDto;
import top.continew.admin.hanfu.domain.query.HanFuTypeQuery;
import top.continew.admin.hanfu.domain.vo.wx.HanfuTypeWxVo;
import top.continew.admin.hanfu.service.impl.HanfuTypeServiceImpl;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/hanfu-type")
@RequiredArgsConstructor
public class HanfuTypeController {

    private final HanfuTypeServiceImpl hanfuTypeService;

    /**
     * 根据汉服类型的条件来查询相应的汉服类型
     * 注：后台系统专用！！！
     **/
    @PostMapping("condition")
    public R getByCondition(@RequestBody HanFuTypeQuery hanFuTypeQuery) {

        Page<HanfuType> hanfuTypes = new Page<>(hanFuTypeQuery.getPageNum(), hanFuTypeQuery.getPageSize());

        if (hanFuTypeQuery.getHanFuTypeName() == null && hanFuTypeQuery.getHanFuTypeId() == null && hanFuTypeQuery
            .getHanFuTypeStatus() == null) {
            hanfuTypeService.page(hanfuTypes);
        } else {

            hanfuTypeService.lambdaQuery()
                .eq(hanFuTypeQuery.getHanFuTypeId() != null, HanfuType::getHanfuTypeId, hanFuTypeQuery.getHanFuTypeId())
                .like(hanFuTypeQuery.getHanFuTypeName() != null, HanfuType::getHanfuTypeName, hanFuTypeQuery
                    .getHanFuTypeName())
                .eq(hanFuTypeQuery.getHanFuTypeStatus() != null, HanfuType::getStatus, hanFuTypeQuery
                    .getHanFuTypeStatus())
                .page(hanfuTypes);
        }

        hanfuTypes.getRecords().forEach(i -> {
            i.setUpdataTime(String.valueOf(DateTime.of(Long.parseLong(i.getUpdataTime()))
                .toString("yyyy-MM-dd HH:mm:ss")));
        });

        return R.success(hanfuTypes);
    }

    /**
     * wx接口
     * 
     * @return
     */
    @SaIgnore
    @GetMapping("/list")
    public R getHanfuTypeWxList() {
        QueryWrapper<HanfuType> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("hanfu_type_id", "hanfu_type_name").eq("status", 1);
        List<HanfuTypeWxVo> hanfuTypeWxVos = BeanUtil.copyToList(hanfuTypeService
            .list(queryWrapper), HanfuTypeWxVo.class);

        return R.success(hanfuTypeWxVos);
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        return R.success(hanfuTypeService.getById(id));
    }

    @PostMapping("addHanfuType")
    public R add(@Valid @RequestBody HanfuTypeDto hanfuType) {
        hanfuType.setUpdataTime(String.valueOf(System.currentTimeMillis()));
        HanfuType hanfu = BeanUtil.copyProperties(hanfuType, HanfuType.class);
        hanfu.setHanfuTypeId(null);
        return R.success(hanfuTypeService.save(hanfu));
    }

    @PostMapping("/updata")
    public R update(@RequestBody HanfuType hanfuType) {

        boolean update = hanfuTypeService.lambdaUpdate()
            .set(HanfuType::getUpdataBy, hanfuType.getUpdataBy())
            .set(HanfuType::getUpdataTime, System.currentTimeMillis())
            .set(HanfuType::getHanfuTypeName, hanfuType.getHanfuTypeName())
            .set(HanfuType::getStatus, hanfuType.getStatus())
            .eq(HanfuType::getHanfuTypeId, hanfuType.getHanfuTypeId())
            .update();

        return update ? R.success() : R.fail("更新失败");
    }

    /**
     * 已经废弃了
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        return R.success(hanfuTypeService.removeById(id));
    }

    @PutMapping("alldel")
    public R deleteAll(@RequestBody ArrayList<Integer> arrayList) {
        return R.success(hanfuTypeService.removeByIds(arrayList));
    }

}
